/*************************************************************************
	> File Name: P1746.cpp
	> Author: yufangze
	> Mail: 2224590123@qq.com
	> Created Time: 2025-02-18 17:20:49
 ************************************************************************/
#include <iostream>
#include <utility>
#include <queue>
using namespace std;

#define MAX_N 1000
int mp[MAX_N + 5][MAX_N + 5];
int dis[MAX_N + 5][MAX_N + 5];
int s1, s2, e1, e2, n;
typedef pair<int, int> Post;
bool inQueue[MAX_N + 5][MAX_N + 5];
int dir[4][2] = {
    {1, 0}, {-1, 0}, {0, 1}, {0, -1}
};

bool check(int x, int y) {
    if (x < 1 || x > n || y < 1 || y > n || inQueue[x][y]) return 0;
    if (mp[x][y] == 1) return 0;
    return 1;
}

int dfs(int x, int y) {
    queue<Post> q;
    q.push(Post(x, y));
    inQueue[x][y] = 1;
    dis[x][y] = 0;
    while (!q.empty()) {
        Post front = q.front();
        q.pop();
        if (front.first == e1 && front.second == e2) return dis[e1][e2];
        for (int k = 0; k < 4; k++) {
            int dx = front.first + dir[k][0];
            int dy = front.second + dir[k][1];
            if (!check(dx, dy)) continue;
            inQueue[dx][dy] = 1;
            dis[dx][dy] = dis[front.first][front.second] + 1;
            q.push(Post(dx, dy));
        }
    }
    return -1;
}

int main() {
    cin >> n;
    for (int i = 1; i <= n; i++) {
        string str;
        cin >> str;
        for (int j = 0; j < str.size(); j++) {
            if (str[j] == '0') mp[i][j + 1] = 0;
            else mp[i][j + 1] = 1;
        }
    }
    cin >> s1 >> s2 >> e1 >> e2;
    cout << dfs(s1, s2) << endl;;
    return 0;
}
